我可以減少列表視圖的執行時間嗎? (Can i reduce execution time of listview?)


問題描述

我可以減少列表視圖的執行時間嗎? (Can i reduce execution time of listview?)

I am Using following function to Add items.this function working proparly but it takes much time to execution.please help me to reduce the execution time of items add in listview.

Function:

listViewCollection.Clear();
listViewCollection.LargeImageList = imgList;
listViewCollection.LargeImageList.ImageSize = new System.Drawing.Size(100, 100);
 foreach (var dr in Ringscode.Where(S => !S.IsSold))
 {
     listViewCollection.Items.Insert(0,
                   new ListViewItem(dr.CodeNo.ToString(), dr.RingID.ToString()));
     imgList.Images.Add(dr.RingID.ToString(), binaryToImage(dr.Image));
 }

public Image binaryToImage(System.Data.Linq.Binary binary) 
{ 
   byte[] b = binary.ToArray(); 
   MemoryStream ms = new MemoryStream(b); 
   Image img = Image.FromStream(ms); 
   return img; 
}

參考解法

方法 1:

You need to expect that your UI is going to be slow if you do nonUI work in same thread (in your case, manipulating streams and images). Solution would be to offload this work to another thread, leaving UI thread to user. Once worker thread finishes, tell UI thread to do the update.

Second point is that when updating ListView data in batch, you should tell ListView to wait until you finish all manipulation.

Better if you do it like this. Commentary is inline.

// Create a method which will be executed in background thread,
// in order not to block UI
void StartListViewUpdate()
{
    // First prepare the data you need to display
    List<ListViewItem> newItems = new List<ListViewItem>();
    foreach (var dr in Ringscode.Where(S => !S.IsSold))
    {
        newItems.Insert(0,
                      new ListViewItem(dr.CodeNo.ToString(), dr.RingID.ToString()));
        imgList.Images.Add(dr.RingID.ToString(), binaryToImage(dr.Image));
    }

    // Tell ListView to execute UpdateListView method on UI thread
    // and send needed parameters
    listView.BeginInvoke(new UpdateListDelegate(UpdateListView), newItems);
}

// Create delegate definition for methods you need delegates for
public delegate void UpdateListDelegate(List<ListViewItem> newItems);
void UpdateListView(List<ListViewItem> newItems)
{
    // Tell ListView not to update until you are finished with updating it's
    // data source
    listView.BeginUpdate();
    // Replace the data
    listViewCollection.Clear();
    listViewCollection.LargeImageList = imgList;
    listViewCollection.LargeImageList.ImageSize = new System.Drawing.Size(100, 100);
    foreach (ListViewItem item in newItems)
        listViewCollection.Add(item);
    // Tell ListView it can now update
    listView.EndUpdate();
}

// Somewhere in your code, spin off StartListViewUpdate on another thread
...
        ThreadPool.QueueUserWorkItem(new WaitCallback(StartListViewUpdate));
...

You may need to fix a few things as I wrote this inline, and didn't test it in VS.

(by TulsiNikola Radosavljević)

參考文件

  1. Can i reduce execution time of listview? (CC BY-SA 3.0/4.0)

#listview #c#-4.0 #.net #imagelist






相關問題

ListView中的序列號列 (Serial number Column in ListView)

狀態列表可繪製在預蜂窩版本上無法正常工作 (State list drawable not working properly on pre-honeycomb versions)

我可以減少列表視圖的執行時間嗎? (Can i reduce execution time of listview?)

ListView Windows 8 多個索引 (ListView Windows 8 multiple indexes)

如何通過單擊歌曲列表來播放歌曲 (How to play the song by clicking the lists of song)

Thuộc tính chi tiết trong ListView không hiển thị (Details property in ListView not displaying)

如何從 DataModel 動態訪問數據以獲取 Blackberry 10 中的可折疊列表 (How to access data dynamically from DataModel for collapsible list in Blackberry 10)

android如何將listview項目發送到另一個活動 (android How to send listview item to another activity)

實現 RecyclerView (Implementing RecyclerView)

如何刪除列表視圖底部的多餘空間? (How can I remove extraspace in listview bottom?)

ListViewItem ItemSelectionChangedEvent 觸發 4 次 [e.Selected 觸發兩次] 導致 Win32 異常未處理 (ListViewItem ItemSelectionChangedEvent Fires 4 times [e.Selected fires twice] leads to Win32 Exception Unhandled)

Kivy:如何使用 RecycleView 在 Kivy 中顯示具有可變行的列表? (Kivy: how to display a list with variable rows in Kivy with a RecycleView?)







留言討論